import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:toastification/toastification.dart';
import 'my_colors.dart';

/// Toast工具类，提供不同类型的Toast提示
class MyToast {
  /// 展示成功提示
  static void showSuccess(BuildContext context, String message) {
    final successColor = Colors.green.shade600;
    toastification.show(
      context: context,
      type: ToastificationType.success,
      style: ToastificationStyle.flat,
      autoCloseDuration: const Duration(seconds: 3),
      title: Text(
        'Success',
        style: TextStyle(
          color: Colors.white,
          fontSize: 16.sp,
          fontWeight: FontWeight.bold,
        ),
      ),
      description: Text(
        message,
        style: TextStyle(color: Colors.white, fontSize: 14.sp),
      ),
      alignment: Alignment.topCenter,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      margin: EdgeInsets.only(top: 40.h, left: 20.w, right: 20.w),
      borderRadius: BorderRadius.circular(16.r),
      boxShadow: [
        BoxShadow(
          color: successColor.withOpacity(0.3),
          spreadRadius: 2,
          blurRadius: 8,
          offset: const Offset(0, 4),
        ),
      ],
      closeButtonShowType: CloseButtonShowType.onHover,
      icon: Icon(Icons.check_circle_outline, color: Colors.white, size: 24.sp),
      backgroundColor: successColor,
      primaryColor: Colors.white,
      applyBlurEffect: false,
      showProgressBar: false,
    );
  }

  /// 展示错误提示
  static void showError(BuildContext context, String message) {
    final errorColor = Colors.red.shade600;
    toastification.show(
      context: context,
      type: ToastificationType.error,
      style: ToastificationStyle.flat,
      autoCloseDuration: const Duration(seconds: 3),
      title: Text(
        'Error',
        style: TextStyle(
          color: Colors.white,
          fontSize: 16.sp,
          fontWeight: FontWeight.bold,
        ),
      ),
      description: Text(
        message,
        style: TextStyle(color: Colors.white, fontSize: 14.sp),
      ),
      alignment: Alignment.topCenter,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      margin: EdgeInsets.only(top: 40.h, left: 20.w, right: 20.w),
      borderRadius: BorderRadius.circular(16.r),
      boxShadow: [
        BoxShadow(
          color: errorColor.withOpacity(0.3),
          spreadRadius: 2,
          blurRadius: 8,
          offset: const Offset(0, 4),
        ),
      ],
      closeButtonShowType: CloseButtonShowType.onHover,
      icon: Icon(Icons.error_outline, color: Colors.white, size: 24.sp),
      backgroundColor: errorColor,
      primaryColor: Colors.white,
      applyBlurEffect: false,
      showProgressBar: false,
    );
  }

  /// 展示警告提示
  static void showWarning(BuildContext context, String message) {
    final warningColor = Colors.amber.shade700;
    toastification.show(
      context: context,
      type: ToastificationType.warning,
      style: ToastificationStyle.flat,
      autoCloseDuration: const Duration(seconds: 3),
      title: Text(
        'Warning',
        style: TextStyle(
          color: Colors.white,
          fontSize: 16.sp,
          fontWeight: FontWeight.bold,
        ),
      ),
      description: Text(
        message,
        style: TextStyle(color: Colors.white, fontSize: 14.sp),
      ),
      alignment: Alignment.topCenter,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      margin: EdgeInsets.only(top: 40.h, left: 20.w, right: 20.w),
      borderRadius: BorderRadius.circular(16.r),
      boxShadow: [
        BoxShadow(
          color: warningColor.withOpacity(0.3),
          spreadRadius: 2,
          blurRadius: 8,
          offset: const Offset(0, 4),
        ),
      ],
      closeButtonShowType: CloseButtonShowType.onHover,
      icon: Icon(
        Icons.warning_amber_outlined,
        color: Colors.white,
        size: 24.sp,
      ),
      backgroundColor: warningColor,
      primaryColor: Colors.white,
      applyBlurEffect: false,
      showProgressBar: false,
    );
  }

  /// 展示信息提示
  static void showInfo(BuildContext context, String message) {
    final infoColor = MyColors.dartBlue;
    toastification.show(
      context: context,
      type: ToastificationType.info,
      style: ToastificationStyle.flat,
      autoCloseDuration: const Duration(seconds: 3),
      title: Text(
        'Information',
        style: TextStyle(
          color: Colors.white,
          fontSize: 16.sp,
          fontWeight: FontWeight.bold,
        ),
      ),
      description: Text(
        message,
        style: TextStyle(color: Colors.white, fontSize: 14.sp),
      ),
      alignment: Alignment.topCenter,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      margin: EdgeInsets.only(top: 40.h, left: 20.w, right: 20.w),
      borderRadius: BorderRadius.circular(16.r),
      boxShadow: [
        BoxShadow(
          color: infoColor.withOpacity(0.3),
          spreadRadius: 2,
          blurRadius: 8,
          offset: const Offset(0, 4),
        ),
      ],
      closeButtonShowType: CloseButtonShowType.onHover,
      icon: Icon(Icons.info_outline, color: Colors.white, size: 24.sp),
      backgroundColor: infoColor,
      primaryColor: Colors.white,
      applyBlurEffect: false,
      showProgressBar: false,
    );
  }

  /// 展示加载提示
  static ToastificationItem showLoading(BuildContext context, String message) {
    // 先关闭所有可能存在的提示，避免叠加
    dismissAll();

    final loadingColor = MyColors.blue.withOpacity(0.9);

    // 短暂延迟后显示新的加载提示
    return toastification.show(
      context: context,
      type: ToastificationType.info,
      style: ToastificationStyle.flat,
      autoCloseDuration: const Duration(seconds: 30), // 设置一个最大持续时间，防止永久显示
      title: Text(
        'Loading',
        style: TextStyle(
          color: Colors.white,
          fontSize: 16.sp,
          fontWeight: FontWeight.bold,
        ),
      ),
      description: Text(
        message,
        style: TextStyle(color: Colors.white, fontSize: 14.sp),
      ),
      alignment: Alignment.topCenter,
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 14.h),
      margin: EdgeInsets.only(top: 40.h, left: 20.w, right: 20.w),
      borderRadius: BorderRadius.circular(16.r),
      boxShadow: [
        BoxShadow(
          color: loadingColor.withOpacity(0.3),
          spreadRadius: 2,
          blurRadius: 10,
          offset: const Offset(0, 4),
        ),
      ],
      closeButtonShowType: CloseButtonShowType.none,
      icon: SizedBox(
        height: 24.sp,
        width: 24.sp,
        child: CircularProgressIndicator(
          color: Colors.white,
          strokeWidth: 2.5.w,
          valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
        ),
      ),
      backgroundColor: loadingColor,
      primaryColor: Colors.white,
      applyBlurEffect: false,
      showProgressBar: false,
    );
  }

  /// 关闭特定的Toast
  static void dismiss(ToastificationItem item) {
    // 确保在UI线程上执行关闭操作
    Future.microtask(() {
      try {
        toastification.dismiss(item);
      } catch (e) {
        // 如果关闭失败，尝试关闭所有提示
        dismissAll();
        debugPrint('Error dismissing toast: $e');
      }
    });
  }

  /// 关闭所有Toast
  static void dismissAll() {
    Future.microtask(() {
      toastification.dismissAll();
    });
  }
}
